package com.sanpu.kxe.business.trade.view;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.business.member.provider.IMemberProvider;
import com.sanpu.kxe.business.trade.adapter.ChoosePayAdapter;
import com.sanpu.kxe.business.trade.bean.PayCollectionOutput;
import com.sanpu.kxe.business.trade.contract.ChoosePayDetailsContract;
import com.sanpu.kxe.business.trade.contract.TradeConfig;
import com.sanpu.kxe.business.trade.presenter.ChoosePayDetailsPresenter;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.view.NoScrollListView;
import com.sanpu.kxe.module.immersionbar.ImmersionBar;
import com.sanpu.kxe.module.roundview.RoundTextView;
import com.sanpu.kxe.utils.PublicProviderUtil;
import com.sanpu.kxe.utils.SystemBarHelper;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Description：收款选择支付渠道
 * Created by 薛勇军 on 2019/2/28.
 */

public class ChoosePayDetailsActivity extends BaseTitleActivity implements ChoosePayDetailsContract.View {

    @Bind(R.id.recyclerView)
    NoScrollListView payListView;
    @Bind(R.id.recyclerView2)
    NoScrollListView quickListView;
    @Bind(R.id.typeRtv)
    RoundTextView typeRtv;
    @Bind(R.id.payMoneyTv)
    TextView payMoneyTv;
    @Bind(R.id.quickpay_layout)
    LinearLayout quickpayLayout;
    @Bind(R.id.pay_layout)
    LinearLayout payLayout;
    @Bind(R.id.lvTitle)
    TextView lvTitle;
    @Bind(R.id.upgradeRtv)
    RoundTextView upgradeRtv;
    @Bind(R.id.rateTv)
    TextView rateTv;
    @Bind(R.id.headerView)
    RelativeLayout headerView;

    private ChoosePayAdapter payAdapter;

    private ChoosePayDetailsContract.Presenter mPresenter;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_choose_pay_details;
    }

    @Override
    protected void setListener() {
        super.setListener();
        ButterKnife.findById(this, R.id.leftIv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        payListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mPresenter.onItemClickListener(((ChoosePayAdapter) parent.getAdapter()).getItem(position));
            }
        });
        upgradeRtv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.openMembershipDetailsActivity();
            }
        });
        rateTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IMemberProvider provider = PublicProviderUtil.getProvider(IMemberProvider.class);
                if (provider != null) {
                    provider.openMemberProductRateActivity(mContext);
                }
            }
        });
    }

    @Override
    protected void initView() {
        super.initView();
        int barHeight = SystemBarHelper.getStatusBarHeight(getContext());
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) headerView.getLayoutParams();
        params.setMargins(0, barHeight, 0, 0);
        headerView.setLayoutParams(params);
    }

    @Override
    protected void initData() {
        super.initData();
        String mReqAmount = "", reqType = "";
        if (getIntent().hasExtra(TradeConfig.INTENT_REQAMOUNT)) {
            mReqAmount = getIntent().getStringExtra(TradeConfig.INTENT_REQAMOUNT);
            if (!TextUtils.isEmpty(mReqAmount))
                payMoneyTv.setText(new DecimalFormat("#0.00").format(new Double(mReqAmount)));
        }
        if (getIntent().hasExtra(TradeConfig.INTENT_REQTYPE)) {
            reqType = getIntent().getStringExtra(TradeConfig.INTENT_REQTYPE);
            if (TextUtils.equals(TradeConfig.GATHERING_QUICKPAY, reqType)) {
                typeRtv.setText("个人收款");
            } else {
                typeRtv.setText("商家收款");
            }
        }

        UserCenterOutput mCenterEntity = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        String chkSts = CommonConfig.AUTHEN_NOTSUB;
        if (mCenterEntity != null) {
            chkSts = mCenterEntity.getChkSts();
        }

        upgradeRtv.setVisibility(View.GONE);
        payAdapter = new ChoosePayAdapter(null, mContext);
        payListView.setAdapter(payAdapter);

        mPresenter = new ChoosePayDetailsPresenter(this);
        mPresenter.initData(mReqAmount, reqType, chkSts);
    }

    @Override
    public void showListDataView(List<PayCollectionOutput> payListData) {
        payAdapter.setNotifyData(payListData);
        if (payListData != null && payListData.size() > 0) {
            payLayout.setVisibility(View.VISIBLE);
        } else {
            payLayout.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.statusBarColorTransformEnable(true)
                .navigationBarWithKitkatEnable(false)
                .init();
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    protected boolean hasCommonTitle() {
        return false;
    }

    @Override
    public void setPresenter(ChoosePayDetailsContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
